//
//  SuggestCollectionManager.m
//  Project_B
//
//  Created by 甘霞 on 15/11/28.
//  Copyright © 2015年 Atom_Wang. All rights reserved.
//

#import "SuggestCollectionManager.h"

@interface SuggestCollectionManager ()

@property (nonatomic, retain) NSMutableArray *arr;

@end

@implementation SuggestCollectionManager

+ (BOOL)isSuggestModelExist:(NSString *)title {
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"SuggestModel" inManagedObjectContext:[[CoreDataManager shareInstance] managedObjectContext]];
    [fetchRequest setEntity:entity];
    // Specify criteria for filtering which objects to fetch
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title = %@", title];
    [fetchRequest setPredicate:predicate];
    // Specify how the fetched objects should be sorted
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                        initWithKey:@"title"
                                        ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [[[CoreDataManager shareInstance] managedObjectContext] executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        return NO;
    }
    return YES;
}

+ (BOOL)insertSuggestModelTitle:(NSString *)title   //文章标题
                           desc:(NSString *)desc    //文章副标题
                  mainImageView:(NSString *)mainImageView    //主要图片
                       item_url:(NSString *)item_url
                    authorImage:(NSString *)avatar  //作者头像
                     authorName:(NSData *)name    //作者名字
                      poemTitle:(NSData *)poemtitle   //诗名
                      signature:(NSData *)signature   //签名
                       typeName:(NSString *)type_name   //  类型
                      suggestId:(NSNumber *)suggestId {

    //  如果已经存在 不增加
    if ([self isSuggestModelExist:title]) {
        return NO;
    }
    
    SuggestModel *suggestModel = [NSEntityDescription insertNewObjectForEntityForName:@"SuggestModel"
                                                               inManagedObjectContext:[[CoreDataManager shareInstance] managedObjectContext]];
    suggestModel.title = title;
    suggestModel.intro = desc;
    suggestModel.share_text = mainImageView;
    suggestModel.item_url = item_url;
    suggestModel.share_image = avatar;
    suggestModel.thumb = name;
    suggestModel.content = poemtitle;
    suggestModel.cp = signature;
    suggestModel.type_name = type_name;
    suggestModel.suggest_id = suggestId;
    
    [[CoreDataManager shareInstance] saveContext];
    
    return YES;
}

+ (BOOL)deleteSuggestModel:(NSString *)title {

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"SuggestModel" inManagedObjectContext:[[CoreDataManager shareInstance] managedObjectContext]];
    [fetchRequest setEntity:entity];
    // Specify criteria for filtering which objects to fetch
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title = %@", title];
    [fetchRequest setPredicate:predicate];
    // Specify how the fetched objects should be sorted
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [[[CoreDataManager shareInstance] managedObjectContext] executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"没有这个数据");
        return NO;
    }
    
    NSMutableArray *arr = [NSMutableArray arrayWithArray:fetchedObjects];
    SuggestModel *suggestModel = arr[0];
    [[[CoreDataManager shareInstance] managedObjectContext] deleteObject:suggestModel];
    
    [[CoreDataManager shareInstance] saveContext];
    return YES;
}

@end
